<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button>按钮 1</button>
    <button>按钮 2</button>
    <button>按钮 3</button>
  
    <script>
        // 获取所有按钮元素
        var buttons = document.getElementsByTagName('button');
        // 遍历按钮元素并添加事件监听器
        for (let i = 0; i < buttons.length; i++) {
            buttons[i].addEventListener('click', function () {
            alert(i);
            });
        }
    </script>
</body>
</html>